<template>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
    
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { signup } from '@/api/user'
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },

    async handleSubmit() {
      // let { username, password } = this.form
      // let result = await signup(`username=${username}&password=${password}`)
      let { data: { data: { message }, ret } } = await signup(this.form)
      if (ret) {
        this.dialogVisible = false

        // 刷新父组件
        this.$parent._loadData()

        // 清空表单
        this.form.username = ''
        this.form.password = ''
      } else {
        console.log(message)
      }
    }
  }
}
</script>

<style lang='stylus' scoped>

</style>